<?php

if (!defined('NV_SYSTEM')) { die ("You can't access this file directly..."); }

require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
if (file_exists("".$datafold."/story_config.php")) {
	@require_once ("".$datafold."/story_config.php");
}
if (defined('_MODTITLE')) $module_title = _MODTITLE;
//hien thi block phai: 1= co, 0 = khong
$index = 0;

function main() {
	global $db, $prefix, $currentlang, $language, $multilingual, $module_name, $datafold;
	include("header.php");
	include("$datafold/story_config.php");
	
    if ($multilingual == 1) {
        $querylang = "AND (alanguage='$currentlang' OR alanguage='')";
    } else {
        $querylang = "";
    }
    
    $res = $db->sql_query("SELECT cid, title, strcount FROM ".$prefix."_truyen_cat WHERE strcount>'0' ORDER BY weight");
    $tdwidth = intval(100/$cat_table);
	echo "<table align=\"\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"100%\"><tr>";
	$tb = 0;
    while(list($cid, $title_cat, $strcount) = $db->sql_fetchrow($res)) {
    echo "<td valign=\"top\" width=\"".$tdwidth."%\" align=\"left\">"; 	
    echo"<a href=\"modules.php?name=$module_name&td=cat&catid=$cid\"><img border=\"0\" src=\"modules/$module_name/images/folder.gif\" align=\"absmiddle\" width=\"16\"><b>$title_cat<font color=\"#ff0000\" face=\"arial\" size=\"2\"> ($strcount)</font></b></a><br> ";
    echo"</td>";
	$tb ++;
	if($tb == $cat_table) { $tb = 0; echo "</td></tr><tr>"; }
    else { echo "</td>"; }
	}
	echo"</table>";
	if($allow_post !=0 ) {
	echo"<div align=\"right\"><img border=\"0\" src=\"modules/$module_name/images/post.gif\" align=\"absmiddle\">&nbsp;<a href=\"modules.php?name=$module_name&td=links_post\"><b>"._LINKS_POST."</b></a></div>";
	}
	echo"<hr style=\"border: 1px dotted #C0C0C0\" size=\"1\">";
	
	$res_tr = $db->sql_query("SELECT id, cid, title, hometext, images FROM ".$prefix."_truyen WHERE title!='' $querylang ORDER BY title   LIMIT $numstr_home");
	if($db->sql_numrows($res_tr) > 0) {
	echo "<table border=\"0\" width=\"100%\" cellpadding=\"3\" style=\"border-collapse: collapse\">\n";
	echo "	<tr>\n";

//hien thi : Gioi thieu DN theo thu tu abc
//	echo "		<td><img border=\"0\" src=\"modules/$module_name/images/new.gif\" align=\"absmiddle\" width=\"28\"><b><u>"._NEWSTR.":</u></b></td>\n";
	echo "		<td><font color=\"#ff0000\"  size=\"2\"><b><u>"._NEWSTR.":</u></b></font><BR><br></td>\n";

	echo "	</tr>\n";	
	while(list($id, $cid, $title_truyen, $hometext, $images) = $db->sql_fetchrow($res_tr)) {
	if ($images != "" && (file_exists("$thumb_path_pic/$images"))) {
    $str_pic = "<table border=\"0\" width=\"$sizenews\" cellpadding=\"0\" cellspacing=\"0\" align=\"left\">\n<tr>\n<td>\n<img border=\"0\" src=\"$thumb_path_pic/$images\" width=\"$sizenews\" style=\"border: 1px solid #FFFFFF\"></td>\n</tr>\n</table>\n";
    } 
	//hien thi anh cua loai linh vuc hoat dong
	else {
    $str_pic = "<table border=\"0\" width=\"$sizenews\" cellpadding=\"0\" cellspacing=\"0\" align=\"left\">\n<tr>\n<td>\n<img border=\"0\" src=\"modules/$module_name/images/cat_default.gif\" width=\"$sizenews\" style=\"border: 1px solid #ACB7C8\"></td>\n</tr>\n</table>\n";
    }	

	//lay logo cua DN
	if ($images != "" && file_exists("$path_pic/$images")) {
    $size2 = @getimagesize("".$path_pic."/".$images."");
    if (file_exists("$thumb_path_pic/$images")) { $xpath = "$thumb_path_pic"; } else { $xpath = "$path_pic"; }
    if($size2[0]  > $sizenews) {
    $photo = "<a href=\"#\" onClick=\"MM_openBrWindow('modules.php?name=$module_name&amp;td=viewphoto&amp;id=$id','','scrollbars=no,width=$size2[0],height=$size2[1],resizable=1')\" onMouseOut=\"window.status=''; return true;\" onMouseOver=\"window.status='"._VIEWIMG."'; return true;\"><img border=\"0\" src=\"$xpath/$images\" width=\"60\"  height=\"60\" style=\"border: 1px solid #FFFFFF\"></a>";

    }else {
    $photo = "<img border=\"0\" src=\"$xpath/$images\"  width=\"60\"  height=\"60\"  style=\"border: 1px solid #FFFFFF\">";	
    }	
    } else {
    $photo = "";
    }
//	$photo = $xpath/$images;
	
	$res_cat = $db->sql_query("SELECT title FROM ".$prefix."_truyen_cat WHERE cid='$cid'");
	list($cat_title) = $db->sql_fetchrow($res_cat);	
	echo "	<tr>\n";

//	echo "		<td>$str_pic<b><a href=\"modules.php?name=$module_name&td=view_article&id=$id\" class=\"storytitle\">$title_truyen</a></b> ($cat_title)<br>$hometext";
//	echo "		<td>$str_pic<b><a href=\"modules.php?name=$module_name&td=view_article&id=$id\" class=\"storytitle\">$title_truyen</a></b><br>$hometext $photo";

echo "<td><table bgcolor=\"#5e81cf\" border=\"0\" cellspacing=\"1\" height=\"0\" width=\"580\">
		<tr>
			<td bgcolor=\"#eff3ff\" width=\"580\" colspan=\"2\"><b><a href=\"modules.php?name=$module_name&td=view_article&id=$id\" >$title_truyen</a></b></td>
		</tr>
		<tr>
			<td bgcolor=\"#eff3ff\" height=\"53\" width=\"80\">
			<div align=\"center\" vlign=\"center\">$photo
				</div>
			</td>
			<td bgcolor=\"#eff3ff\" height=\"53\" valign=\"top\" width=\"490\">$hometext</td>
		</tr>
	</table>";


//	echo"<div align=\"right\"><a href=\"modules.php?name=$module_name&td=view_article&id=$id\" class=\"storytitle\"><img border=\"0\" src=\"modules/$module_name/images/xemtiep.gif\" width=\"43\" height=\"9\"></a></div>";

//	echo"<hr style=\"border: 1px dotted #C0C0C0\" size=\"1\">";

	echo "</td>\n</tr>\n";
	}
	echo"</table>";	
	}
	include("footer.php");	
}	

function view_article($id) {
	global $db, $prefix, $module_name, $datafold;
	include("$datafold/story_config.php");
	$id = intval($id);
	if($id =="" || $id ==0) {
	header("Location: index.php");
	exit;
	}	
	include("header.php");
	$db->sql_query("update ".$prefix."_truyen  set hits=hits+1 where id='$id'");
	$res = $db->sql_query("SELECT cid, title, nguon, hometext, bodytext, images, hits FROM ".$prefix."_truyen WHERE id='$id'");
	list($cid, $title, $nguon, $hometext, $bodytext, $images, $hits) = $db->sql_fetchrow($res);
	if ($images != "" && file_exists("$path_pic/$images")) {
    $size2 = @getimagesize("".$path_pic."/".$images."");
    if (file_exists("$thumb_path_pic/$images")) { $xpath = "$thumb_path_pic"; } else { $xpath = "$path_pic"; }
    if($size2[0]  > $sizenews) {
    $photo = "<a href=\"#\" onClick=\"MM_openBrWindow('modules.php?name=$module_name&amp;td=viewphoto&amp;id=$id','','scrollbars=no,width=$size2[0],height=$size2[1],resizable=1')\" onMouseOut=\"window.status=''; return true;\" onMouseOver=\"window.status='"._VIEWIMG."'; return true;\"><img border=\"0\" src=\"$xpath/$images\" style=\"border: 1px solid #FFFFFF\"></a>\n";
    }else {
    $photo = "<img border=\"0\" src=\"$xpath/$images\" style=\"border: 1px solid #FFFFFF\">\n";	
    }	
    } else {
    $photo = "";
    }
//	echo "<table border=\"0\" width=\"100%\" cellpadding=\"3\" style=\"border-collapse: collapse\">\n";
//	echo "	<tr>\n";
//	echo "		<td class=\"storytitle\">$title</td>\n";
//	echo "	</tr>\n";
//	echo "	<tr>\n";
//	echo "		<td>";
//	echo"(<i>$hits "._VIEWS."</i>)<br><br>$photo$hometext<br>$bodytext";
echo '<div align="center">
	<table style="border-collapse: collapse;" border="0" cellpadding="0" width="100%">
		<tr>
			<td><b><font color="#ff0000" face="Arial" size="2">DOANH NGHI&#7878;P 
			T&#7920; GI&#7898;I THI&#7878;U</font></b><hr></td>
		</tr>
	</table>
</div>';

echo "<table style=\"border-collapse: collapse;\" border=\"0\" bordercolor=\"#111111\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
						<tr>
							<td width=\"70%\" align=\"center\">
							<b><font color=\"#008000\" face=\"Arial\" size=\"5\">$title</font></b></td>
							<td align=\"left\" width=\"100%\">
							<b><font size=\"2\">
							$photo</font></td>
						</tr>
						<tr><td colspan=\"2\"  style=\"padding-left: 35px\" valign=\"top\">
					<br><b>$hometext</b><br></td></tr>
						<tr><td colspan=\"2\">
					<br>$bodytext<br><br><br>";


	if($nguon) {
	echo"<div align=\"right\">(<i>$nguon</i>)</div>";
	}
	echo"</td>\n";
	echo "	</tr>\n";
	echo "</table>\n";
echo "	<br><br>";
	$res_ccd = $db->sql_query("SELECT id, title, hometext, images FROM ".$prefix."_truyen WHERE id!='$id' AND cid='$cid' ORDER BY id DESC LIMIT $numstr_ccd");
	if($db->sql_numrows($res_ccd) > 0) {
	echo"<hr style=\"border: 1px dotted #808000\" size=\"1\">";
	echo"<b>"._STR_CCD.":</b><br>";
	while(list($id_ccd, $title_ccd, $hometext_ccd, $images_ccd) = $db->sql_fetchrow($res_ccd)) {
	echo"<a href=\"modules.php?name=$module_name&td=view_article&id=$id_ccd\">&raquo; $title_ccd</a><br>";
	}	
	}
	quick_jump();
	include("footer.php");	
}	

function quick_jump() {
	global $db, $prefix, $module_name;
	echo"<div align=\"right\">";
	echo "<select  onChange=\"top.location.href=this.options[this.selectedIndex].value\">\n";
	echo "<option value=\"\"  selected>"._QUICK_JUMP."</option>\n";
	$res = $db->sql_query("SELECT cid, title FROM ".$prefix."_truyen_cat WHERE strcount>'0' ORDER BY weight");
	while(list($cid, $title) = $db->sql_fetchrow($res)) {
	echo "<option value=\"modules.php?name=$module_name&td=cat&catid=$cid\">$title</option>\n";
	}
	echo "</select>\n";
	echo"</div>";
}


function cat($catid) {
	global $db, $prefix, $module_name, $datafold;
	include("$datafold/story_config.php");
	$catid = intval($catid);
	if($catid ==0 || $catid =="") {
	header("Location: modules.php?name=$module_name");
	exit;
	}	
	include("header.php");
	OpenTable();
	$numstories = $db->sql_fetchrow($db->sql_query("SELECT COUNT(*) FROM ".$prefix."_truyen WHERE cid='$catid'"));
    $all_page = ( $numstories[0] ) ? $numstories[0] : 1;
    $per_page = $nums_perpage;
    $page = ( isset($_GET['page']) ) ? intval($_GET['page']) : 0;
	$res_tr = $db->sql_query("SELECT id, cid, title, hometext, images FROM ".$prefix."_truyen WHERE cid='$catid' $querylang ORDER BY id DESC LIMIT $page, $per_page");
	if($db->sql_numrows($res_tr) > 0) {
	$res_cat = $db->sql_query("SELECT title FROM ".$prefix."_truyen_cat WHERE cid='$catid'");
	list($cat_title) = $db->sql_fetchrow($res_cat);	
	echo "<table border=\"0\" width=\"100%\" cellpadding=\"3\" style=\"border-collapse: collapse\">\n";
	echo "	<tr>\n";
	echo "		<td><b>"._INCID.": $cat_title</b></td>\n";
	echo "	</tr>\n";	
	while(list($id, $cid, $title_truyen, $hometext, $images) = $db->sql_fetchrow($res_tr)) {
	if ($images != "" && (file_exists("$thumb_path_pic/$images"))) {
    $str_pic = "<table border=\"0\" width=\"$sizenews\" cellpadding=\"0\" cellspacing=\"0\" align=\"left\">\n<tr>\n<td>\n<img border=\"0\" src=\"$thumb_path_pic/$images\" width=\"$sizenews\" style=\"border: 1px solid #FFFFFF\"></td>\n</tr>\n</table>\n";
    }
	//hien thi anh cua loai linh vuc hoat dong
//else {
//    $str_pic = "<table border=\"0\" width=\"$sizenews\" cellpadding=\"0\" cellspacing=\"0\" align=\"left\">\n<tr>\n<td>\n<img border=\"0\" src=\"modules/$module_name/images/cat_default.gif\" width=\"$sizenews\" style=\"border: 1px solid #ACB7C8\"></td>\n</tr>\n</table>\n";
//    }	
	echo "	<tr>\n";
	echo "		<td>$str_pic<b><a href=\"modules.php?name=$module_name&td=view_article&id=$id\" class=\"storytitle\">$title_truyen</a></b><br>$hometext";
	echo"<div align=\"right\"><a href=\"modules.php?name=$module_name&td=view_article&id=$id\" class=\"storytitle\"><img border=\"0\" src=\"modules/$module_name/images/xemtiep.gif\" width=\"43\" height=\"9\"></a></div>";
	echo"<hr style=\"border: 1px dotted #C0C0C0\" size=\"1\"></td>\n";
	echo "	</tr>\n";
	}
	echo"</table>";	
	echo generate_page("modules.php?name=$module_name&td=cat&catid=$catid", $all_page, $per_page, $page);
	}else{
	echo"<center>"._NODATA."</center>";
	}	
	quick_jump();
	CloseTable();
	include("footer.php");
}		

function viewphoto($id) {
	global $db, $prefix, $module_name, $datafold;
	include("$datafold/story_config.php");
	$id = intval($id);
	if($id ==0 || $id =="") {
	header("Location: modules.php?name=$module_name");
	exit;
	}	
	$res = $db->sql_query("SELECT images FROM ".$prefix."_truyen WHERE id='$id'");
	list($images) = $db->sql_fetchrow($res);
	if ($images != "" && (file_exists("$thumb_path_pic/$images") || file_exists("$path_pic/$images"))) {
    if (file_exists("$thumb_path_pic/$images")) { $xpath = "$thumb_path_pic"; } else { $xpath = "$path_pic"; }
    $link ="$path_pic/$images";
    } else {
    $link = "";
    }
	echo"<html>\n<head>\n"
  . "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="._CHARSET."\">\n"
  . "<title>".$module_name." - "._VIEWIMG."</title>\n</head>\n<body bgcolor=\"#ECF3FB\" topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\n"
  . "<p><a href=\"\" onclick=\"window.close();\"><img border=\"0\" src=\"$link\" alt=\""._CLOSEWIN."\"></a></p>\n"
  . "</body>\n</html>";
}	



switch($td) {
	default:
	main();
	break;
	
	case "cat":
	cat($catid);
	break;
	
	case "view_article":
	view_article($id);
	break;
	
	case "viewphoto":
	viewphoto($id);
	break;
}    
    
    
    
    
?>
